//
//  VideoViewController.m
//  MagicCube
//
//  Created by 蓓蕾 on 2021/12/31.
//

#import "VideoViewController.h"
#import "AppDelegate.h"
#import <SJVideoPlayer/SJVideoPlayer.h>
#import <SJBaseVideoPlayer/SJPlaybackRecordSaveHandler.h>
#import <SJUIKit/NSAttributedString+SJMake.h>

#import "SJFloatSmallViewTransitionController.h"
#import <AVKit/AVPictureInPictureController.h>

@interface VideoViewController ()
//@property (nonatomic, strong, nullable) SJVideoModel *media;

@property (nonatomic, strong) SJVideoPlayer *player;

@end

@implementation VideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [AppDelegate shareAppDelegate].allowRotation = YES;
    self.view.backgroundColor = UIColor.whiteColor;
    
    
    // 请根据自己的业务模型来操作, 此处为模拟当前登录的用户
    NSInteger userId = 50;
    
    // 请根据自己的业务模型来操作, 此处为模拟服务器返回的数据模型
//    _media = SJVideoModel.alloc.init;
//    _media.id = 50;
//    _media.URL = SourceURL2;
//    _media.mediaTitle = @"Hello world!";
    
    // 步骤1: 查询播放记录(如不存在, 将返回nil)
    SJPlaybackRecord *record = [SJPlaybackHistoryController.shared recordForMedia:_videoID user:userId mediaType:SJMediaTypeVideo];
    // - 不存在则创建
    if ( record == nil ) {
        record = SJPlaybackRecord.alloc.init;
        record.mediaId = _videoID;
        record.userId = userId;
        record.mediaType = SJMediaTypeVideo;
        // - 添加一些扩展属性, 便于开发者的业务逻辑开发
//        record.title = _videoName;
    }
    
    
    self.player = SJVideoPlayer.player;
    [self.view addSubview:self.player.view];
    self.player.view.frame = CGRectMake(0, 200, kScreenWidth, 250);
    SJVideoPlayerURLAsset *asset = [[SJVideoPlayerURLAsset alloc] initWithURL:[NSURL fileURLWithPath:_videoPath] startPosition:record.position];
    asset.record = record;
    self.player.URLAsset = asset;
//    self.player.URLAsset = [[SJVideoPlayerURLAsset alloc] initWithURL:[NSURL fileURLWithPath:_videoPath]];
    self.player.URLAsset.title = _videoName;
    
    _player.floatSmallViewController = SJFloatSmallViewTransitionController.alloc.init;
//    _player.floatSmallViewController = [[AVPictureInPictureController alloc] init];
    
    _player.defaultFloatSmallViewControlLayer.bottomHeight = 35;
    
    SJEdgeControlButtonItem *playItem = [SJEdgeControlButtonItem.alloc initWithTag:101];
    [playItem addAction:[SJEdgeControlButtonItemAction actionWithTarget:self action:@selector(playOrPause)]];
    [_player.defaultFloatSmallViewControlLayer.bottomAdapter addItem:playItem];
    
    // - 如果之前播放过, 这里提示一下用户从上次的位置进行播放
    if (record.position != 0 ) {
        [_player.prompt show:[NSAttributedString sj_UIKitText:^(id<SJUIKitTextMakerProtocol>  _Nonnull make) {
            make.append([NSString stringWithFormat:@"从上次的位置 %@ 处开始播放", [self.player stringForSeconds:record.position]]);
            make.textColor(UIColor.whiteColor);
        }] duration:5];
    }
}

- (void)playOrPause {
    NSLog(@"暂停与播放");
    self.player.isPaused ? [self.player play] : [self.player pauseForUser];
}

- (SJFloatSmallViewTransitionController *_Nullable)SVTC_floatSmallViewTransitionController {
    return (SJFloatSmallViewTransitionController *)_player.floatSmallViewController;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"开启画中画");
    [_player.playbackController startPictureInPicture];
}

-(void)dealloc
{
    NSLog(@"销毁视频播放器");
    [AppDelegate shareAppDelegate].allowRotation = NO;;
    
    // 步骤3: 初始化保存管理类
    SJPlaybackRecordSaveHandler *handler = SJPlaybackRecordSaveHandler.shared;
    // 指定保存的时机, handler 将自动进行保存
    handler.events = SJPlayerEventMaskAll;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
